package map.animation
{
	import flash.geom.Point;
	
	import map.MapView;

	public class MapCenterAnimation extends BaseMapAnimation
	{
		private var _map:MapView;
		private var _center:Point;
		private var _target:Point;
		private var _origin:Point;
		private var _index:int;
		public function MapCenterAnimation(map:MapView,center:Point,priority:int,frames:int = 20)
		{
			super();
			_map = map;
			_origin = new Point(_map.x,_map.y);
			_center = center;
			_priority = priority;
			_index = _frames = frames;
		}
		
		override public function isSame(animation:BaseMapAnimation):Boolean
		{
			return animation is MapCenterAnimation;
		}
		
		override public function prepare():void
		{
			_target = new Point();
			_target.x = _map.viewWidth*.5 - _center.x;
			_target.y = _map.viewHeight*.5 - _center.y;
			if(_target.x > 0)
			{
				_target.x =0;
			}else if(_target.x < _map.viewWidth - _map.width)
			{
				_target.x = _map.viewWidth - _map.width;
			}
			if(_target.y > 0)
			{
				_target.y =0;
			}else if(_target.y < _map.viewHeight-_map.height)
			{
				_target.y = _map.viewHeight-_map.height;
			}
		}
		
		override public function updata():void
		{
			_index--;
			if(_index>0)
			{
				var p:Point = Point.interpolate(_origin,_target,_index/_frames);
				_map.x = p.x;
				_map.y = p.y;
			}else{
				finish();
			}
		}
		
		override public function finish():void
		{
			_finished = true;
			_map.x = _target.x;
			_map.y = _target.y;
			_map = null;
		}
		
		override public function cancle():void
		{
			_finished = true;
			_map = null;
		}
	}
}